package dji.ux.ext.panel.camera;

import androidx.annotation.NonNull;

import dji.common.camera.SettingsDefinitions;
import dji.common.camera.ThermalMeasurementMode;
import dji.common.error.DJIError;
import dji.common.util.CommonCallbacks;
import dji.sdk.camera.Camera;
import dji.sdk.sdkmanager.DJISDKManager;
import dji.ux.beta.base.DJISDKModel;
import dji.ux.beta.base.WidgetModel;
import dji.ux.beta.base.uxsdkkeys.ObservableInMemoryKeyedStore;


/**
 * Widget Model for the {@link CameraMavic2DualModel} used to define
 * the underlying logic and communication
 */
class CameraMavic2DualModel extends WidgetModel {

    private Camera thermalCamera;               //热成像相机


    CameraMavic2DualModel(@NonNull DJISDKModel djiSdkModel,
                          @NonNull ObservableInMemoryKeyedStore keyedStore) {
        super(djiSdkModel, keyedStore);
    }

    boolean isMavic2DualCamera() {
        if (DJISDKManager.getInstance().getProduct() == null ||
                DJISDKManager.getInstance().getProduct().getCameras() == null)
            return false;

        boolean isMavic2DualCamera = false;
        for (Camera camera : DJISDKManager.getInstance().getProduct().getCameras()) {
            if (camera.getDisplayName().equals(Camera.DisplayNameMavic2EnterpriseDual_IR)) {
                thermalCamera = camera;
                isMavic2DualCamera = true;
            }
        }

        return isMavic2DualCamera;
    }


    void getDisplayMode(CommonCallbacks.CompletionCallbackWith<SettingsDefinitions.DisplayMode> callback) {
        if (thermalCamera == null)
            return;

        thermalCamera.getDisplayMode(callback);
    }

    void setDisplayMode(SettingsDefinitions.DisplayMode displayMode, CommonCallbacks.CompletionCallback callback) {
        if (thermalCamera == null)
            return;

        thermalCamera.setDisplayMode(displayMode, callback);
    }

    void setThermalMeasurementMode(ThermalMeasurementMode mode, CommonCallbacks.CompletionCallback callback) {
        if (thermalCamera != null) {
            thermalCamera.setThermalMeasurementMode(mode, callback);
        }
    }

    void getThermalMeasurementMode(CommonCallbacks.CompletionCallbackWith<ThermalMeasurementMode> callback) {
        if (thermalCamera != null) {
            thermalCamera.getThermalMeasurementMode(callback);
        }
    }

    void setFFCMode() {
        if (thermalCamera == null) {
            return;
        }

        thermalCamera.getThermalFFCMode(new CommonCallbacks.CompletionCallbackWith<SettingsDefinitions.ThermalFFCMode>() {
            @Override
            public void onSuccess(SettingsDefinitions.ThermalFFCMode thermalFFCMode) {
                if (thermalFFCMode == SettingsDefinitions.ThermalFFCMode.AUTO) {
                    thermalCamera.setThermalFFCMode(SettingsDefinitions.ThermalFFCMode.MANUAL, null);
                } else if (thermalFFCMode == SettingsDefinitions.ThermalFFCMode.MANUAL) {
                    thermalCamera.setThermalFFCMode(SettingsDefinitions.ThermalFFCMode.AUTO, null);
                }
            }

            @Override
            public void onFailure(DJIError djiError) {

            }
        });
    }

    void getFFCMode(CommonCallbacks.CompletionCallbackWith<SettingsDefinitions.ThermalFFCMode> callbackWith) {
        if (thermalCamera == null) {
            return;
        }

        thermalCamera.getThermalFFCMode(callbackWith);
    }

    @Override
    protected void inSetup() {

    }

    @Override
    protected void inCleanup() {
        //Nothing to cleanup
    }

    @Override
    protected void updateStates() {
        //Nothing to update
    }


}
